792B - Counting-out Rhyme - CodeForces Solution


implementation *1300

Please click on ads to support us..

Python Code:

def solve(n, a, k):
    leader = 0
    size = n
    p = list(range(1, n+1))
    res = []
    
    for i in range(0, k):
        j = (leader+a[i]) % size

        res.append(p[j])
        p.pop(j)

        size -= 1
        leader = j % size

    print(*res)
def main():
    n,k = map(int,input().split())
    a = [int(x) for x in input().split()]

    solve(n, a, k)

if __name__ == "__main__":
    main()

C++ Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <climits>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <ctime>
#include <string>
#include <cstring>
#define lowbit(x) x & (-x)
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int n, k, child[105];
queue <int> children;
int main() {
	cin >> n >> k; 
	for (int i = 1; i <= k; i ++) {
		cin >> child[i];
	}
	for (int i = 1; i <= n; i ++) {
		children.push(i); //进队 
	}
	for(int i = 1;i <= k;i++) {
		child[i] %= children.size();  //防止循环次数过多,而TLE 
		for (int j = 1; j <= child[i]; j ++) {
			children.push (children.front());
			children.pop();
		}
		cout << children.front() << " ";
		children.pop(); //出队 
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

946A - Partition
1068B - LCM
1692E - Binary Deque
679A - Bear and Prime 100
488A - Giga Tower
14A - Letter
1150A - Stock Arbitraging
1552A - Subsequence Permutation
1131F - Asya And Kittens
1475F - Unusual Matrix
133B - Unary
1547A - Shortest Path with Obstacle
624A - Save Luke
1238A - Prime Subtraction
1107C - Brutality
1391B - Fix You
988B - Substrings Sort
312A - Whose sentence is it
513A - Game
1711E - XOR Triangle
688A - Opponents
20C - Dijkstra
1627D - Not Adding
893B - Beautiful Divisors
864B - Polycarp and Letters
1088A - Ehab and another construction problem
1177B - Digits Sequence (Hard Edition)
1155B - Game with Telephone Numbers
1284A - New Year and Naming
863B - Kayaking